package cn.edu.cug.cs.gtl.apis.ui.tree;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreeNode;
import java.util.Hashtable;
import java.util.Vector;

public abstract class Tree extends JTree {
    private DefaultMutableTreeNode currentSelectedNode=null;
    /**
     * Returns a <code>JTree</code> with a sample model.
     * The default model used by the tree defines a leaf node as any node
     * without children.
     *
     */
    public Tree() {
    }

    /**
     * Returns a <code>JTree</code> with each element of the
     * specified array as the
     * child of a new root node which is not displayed.
     * By default, the tree defines a leaf node as any node without
     * children.
     *
     * @param value an array of <code>Object</code>s
     */
    public Tree(Object[] value) {
        super(value);
    }

    /**
     * Returns a <code>JTree</code> with each element of the specified
     * <code>Vector</code> as the
     * child of a new root node which is not displayed. By default, the
     * tree defines a leaf node as any node without children.
     *
     * @param value a <code>Vector</code>
     */
    public Tree(Vector<?> value) {
        super(value);
    }

    /**
     * Returns a <code>JTree</code> created from a <code>Hashtable</code>
     * which does not display with root.
     * Each value-half of the key/value pairs in the <code>HashTable</code>
     * becomes a child of the new root node. By default, the tree defines
     * a leaf node as any node without children.
     *
     * @param value a <code>Hashtable</code>
     */
    public Tree(Hashtable<?, ?> value) {
        super(value);
    }

    /**
     * Returns a <code>JTree</code> with the specified
     * <code>TreeNode</code> as its root,
     * which displays the root node.
     * By default, the tree defines a leaf node as any node without children.
     *
     * @param root a <code>TreeNode</code> object
     */
    public Tree(TreeNode root) {
        super(root);
    }

    /**
     * Returns a <code>JTree</code> with the specified <code>TreeNode</code>
     * as its root, which
     * displays the root node and which decides whether a node is a
     * leaf node in the specified manner.
     *
     * @param root               a <code>TreeNode</code> object
     * @param asksAllowsChildren if false, any node without children is a
     *                           leaf node; if true, only nodes that do not allow
     *                           children are leaf nodes
     */
    public Tree(TreeNode root, boolean asksAllowsChildren) {
        super(root, asksAllowsChildren);
    }

    /**
     * Returns an instance of <code>JTree</code> which displays the root node
     * -- the tree is created using the specified data model.
     *
     * @param newModel the <code>TreeModel</code> to use as the data model
     */
    public Tree(TreeModel newModel) {
        super(newModel);
    }

    public DefaultMutableTreeNode getCurrentSelectedNode() {
        this.currentSelectedNode.getUserObject();
        return currentSelectedNode;
    }

    public TreeNodeData getCurrentSelectedNodeData(){
        if(this.currentSelectedNode==null)
            return null;
        return   (TreeNodeData) this.currentSelectedNode.getUserObject();
    }

    public Object getCurrentReferencedObject(){
        TreeNodeData data = getCurrentSelectedNodeData();
        if(data==null)
            return null;
        return data.getRefObject();
    }

    public void setCurrentSelectedNode(DefaultMutableTreeNode currentSelectedNode) {
        this.currentSelectedNode = currentSelectedNode;
    }
}
